<?php

###########################################################################
#   Copyright (C) 2012 CDNPAL LTD by Christopher Gu                       #
#   resources@cdnpal.com                                                  #
#                                                                         #
#   coded for Php 4.3/5.X and above                                       #
#   and AllowOverride all for .htaccess                                   #
#                                                                         #
#   This program is free software released                                #
#   under the GPL version 2 license:                                      #
#   http://www.gnu.org/licenses/gpl-2.0.htm                               #
#                                                                         #
#   This program is distributed WITHOUT ANY WARRANTY                      #
#   or FITNESS FOR A PARTICULAR PURPOSE.                                  #
#                                                                         #
#   DADF-SDK                                                              #
#                                                                         #
###########################################################################

class OutgoingAcceptedTransformer extends OutgoingTransformer {
	
	public $xmlData;
	
	public function __construct($accepted_response_id_array, $accepted_response_index_authentication = 0, $table_direction = 'Outgoing') {
		
		$member_direction = 'outgoing';
		
		if ($table_direction == 'Incoming'):
			$accepted_response_factory = IncomingAcceptedResponseFactory::get_instance();
			$member_direction = 'incoming';
		else:
			$accepted_response_factory = OutgoingAcceptedResponseFactory::get_instance();
		endif;
		
		if (!is_array($accepted_response_id_array)):
			
			$accepted_response_id_array = array($accepted_response_id_array);
			
		endif;
		
   		$dom_document = new DomDocument("1.0", "ISO-8859-1");

		$dom_element_dadf_accepted 	= $dom_document->createElement('dadf-accepted');

	    $params[$table_direction . "AcceptedResponseID"] = $accepted_response_id_array[$accepted_response_index_authentication]; 
	    $accepted_response_auth = $accepted_response_factory->get_row($params);

		$dom_element_campaignactionresponses 				= $dom_document->createElement('campaignactionresponses');
		
		foreach ($accepted_response_id_array as $accepted_response_id):
		
	    	$params[$table_direction . "AcceptedResponseID"] = $accepted_response_id; 
	    	$accept_response = $accepted_response_factory->get_row($params);
			
			$dom_element_campaignactionresponse 	= $dom_document->createElement('campaignactionresponse');
			
			$dom_element_acceptid			= $dom_document->createElement('acceptid', $accept_response->AcceptID);
			$dom_element_campaignactionresponse->appendChild($dom_element_acceptid);
			$dom_element_id					= $dom_document->createElement('id', $accept_response->ID);
			$dom_element_campaignactionresponse->appendChild($dom_element_id);
			$dom_element_accepted			= $dom_document->createElement('accepted', $this->to_boolean($accept_response->Accepted));
			$dom_element_campaignactionresponse->appendChild($dom_element_accepted);
			$dom_element_reason				= $dom_document->createElement('reason', $accept_response->Reason);
			$dom_element_campaignactionresponse->appendChild($dom_element_reason);

			$dom_element_campaignactionresponses->appendChild($dom_element_campaignactionresponse);
			
		endforeach;
		
		$dom_element_dadf_accepted->appendChild($dom_element_campaignactionresponses);
		$dom_document->appendChild($dom_element_dadf_accepted);
		$dom_document->formatOutput = true;
		
		$dadf_version = Zend_Registry::get('application.version');
		
		if ($dom_document->schemaValidate ('../application/schemas/Dadf.accepted-' . $dadf_version . '.xsd') == false):
			
			throw new Exception("DADF schema validation failed");
			
		endif;
		
		$this->xmlData  = $dom_document->saveXML();
	

	}		
	
}
			